package thesis.filesharing.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import thesis.filesharing.common.exception.system.AppSystemException;
import thesis.filesharing.dao.LecturerDAO;
import thesis.filesharing.dao.util.EMF;
import thesis.filesharing.model.Lecturer;
import thesis.filesharing.model.Role;
import thesis.filesharing.web.auth.Roles;

@Repository
public class LecturerDAOImpl implements LecturerDAO {
	
	@SuppressWarnings("unchecked")
	@Override
	public Lecturer findLecturerById(String lecturerId) {
		EntityManager em = null;
		
		try {
			em = EMF.get().createEntityManager();
			String jpaql = "SELECT l FROM Lecturer l WHERE l.lecturerId = :lecturerId";
			Query query = em.createQuery(jpaql);
			query.setParameter("lecturerId", lecturerId);
			List<Lecturer> lecturers = (List<Lecturer>) query.getResultList();
			if (lecturers != null && !lecturers.isEmpty()) {
				return lecturers.get(0);
			}
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Lecturer saveLecturer(Lecturer lecturer, boolean isAdmin) {
		
		EntityManager em = null;
		EntityTransaction transaction = null;
		
		try {
			em = EMF.get().createEntityManager();
			transaction = em.getTransaction();
			transaction.begin();
			
			// Get ROLE_LECTURER entity in Role kind
			String jpaql = "SELECT r FROM Role r WHERE r.roleName = :roleName";
			Query query = em.createQuery(jpaql);
			query.setParameter("roleName", Roles.ROLE_LECTURER.getRole());
			List<Role> roles = (List<Role>) query.getResultList();
			Role lecturerRole = (roles != null && !roles.isEmpty()) ? roles.get(0) : null;
			lecturer.getRoleKeys().add(lecturerRole.getRoleKey());
			
			// Get ROLE_ADMIN entity if isAdmin = true
			if (isAdmin) {
				query.setParameter("roleName", Roles.ROLE_ADMIN.getRole());
				roles = (List<Role>) query.getResultList();
				Role adminRole = (roles != null && !roles.isEmpty()) ? roles.get(0) : null;
				lecturer.getRoleKeys().add(adminRole.getRoleKey());
			}
			
			// Save lecturer
			em.persist(lecturer);
			em.flush();
			
			transaction.commit();
			
			return lecturer;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Lecturer findLecturerByEmail(String lecturerEmail) {
		EntityManager em = null;
		try {
			em = EMF.get().createEntityManager();
			String jpaql = "SELECT l FROM Lecturer l WHERE l.lecturerEmail = :lecturerEmail";
			Query query = em.createQuery(jpaql);
			query.setParameter("lecturerEmail", lecturerEmail);
			List<Lecturer> lecturers = (List<Lecturer>) query.getResultList();
			if (lecturers != null && !lecturers.isEmpty()) {
				return lecturers.get(0);
			}
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new AppSystemException(ex);
		}
	}
}
